////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2010 Gabriel Montagné Láscaris-Comneno and Alberto
//  Brealey-Guzmán.
//
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package com.rojored.core
{

import com.rojored.core.cacheClasses.DeleteStatus;
import flash.utils.Dictionary;

/**
 *  Implements a basic data cache.
 */
public class Cache implements ICache
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function Cache()
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     *  Core map between keys and values.
     */
    private var map:Dictionary;

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     *  @copy com.rojored.core.ICache#set()
     */
    public function set(key:*, value:*, time:int = 0):Boolean
    {
        if (!map)
            map = new Dictionary();

        map[key] = value;
        return true;
    }

    /**
     *  @copy com.rojored.core.ICache#add()
     */
    public function add(key:*, value:*, time:int = 0):Boolean
    {
        if (!map)
            map = new Dictionary();

        if (key in map)
            return false;

        map[key] = value;
        return true;
    }

    /**
     *  @copy com.rojored.core.ICache#replace()
     */
    public function replace(key:*, value:*, time:int = 0):Boolean
    {
        if (!map)
            return false;

        if (!key in map)
            return false;

        map[key] = value;
        return true;
    }

    /**
     *   @copy com.rojored.core.ICache#get()
     */
    public function get(key:*):*
    {
        return map && map[key];
    }

    /**
     *  @copy com.rojored.core.ICache#flushAll()
     */
    public function flushAll():Boolean
    {
        map = null;
        return true;
    }

    /**
     *  @copy com.rojored.core.ICache#deleteItem()
     */
    public function deleteItem(key:*, seconds:int = 0):int
    {
        if (seconds != 0)
            throw new Error(
                "Timed lock for deleteItem operation is not yet implemented"
            );

        if (!map || !(key in map))
            return DeleteStatus.DELETE_ITEM_MISSING;

        delete map[key];
        return DeleteStatus.DELETE_SUCCESSFUL;
    }
}
}
